package com.nali.dal.statement.function.formatter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.nali.dal.expression.ExpressionValue;
import com.nali.dal.expression.query.Criteria;
import com.nali.dal.statement.function.AbstractDataHandler;
import com.nali.dal.statement.function.FunctionArgument;
import com.nali.dal.statement.function.FunctionException;

/**
 * QueryConds<br>&nbsp;
 * arg1=prop,arg2=criteria,arg3=value
 * arg4=prop,arg5=criteria,arg6=value
 * ....
 * @author sam Created on 2011-5-5
 */
@Component
public class QueryConds extends AbstractDataHandler<List<ExpressionValue<Criteria>>> {

	@Override
	public List<ExpressionValue<Criteria>> handle(List<Object> argList) throws FunctionException {
		List<ExpressionValue<Criteria>> rlt = new ArrayList<ExpressionValue<Criteria>>(argList.size()/3);
		for (int i = 3; i <= argList.size(); i+=3) {
			String prop = (String) argList.get(i-3);
			Criteria criteria = Criteria.valueOf((String)argList.get(i-2));
			Serializable value = (Serializable) argList.get(i-1);
			rlt.add(new ExpressionValue<Criteria>(prop, criteria, value));
		}
		return rlt;
	}

	@Override
	public void validate(List<FunctionArgument> arguments) throws FunctionException {
		if(arguments.size()==0) {
			throw new FunctionException("Datahandler: " + this.getClass() + ",argumetns' size is 0");
		}
		if(arguments.size()%3!=0) {
			throw new FunctionException("Datahandler: " + this.getClass() + ",argumetns' size %3 is not 0");
		}
	
	}
}
